/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package cn.ac.iie.di.zhihu.gkfb.utils;

import cn.ac.iie.di.zhihu.gkfb.common.RuntimeEnv;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import org.apache.commons.io.FileUtils;
import org.apache.log4j.Logger;

/**
 *
 * @author YG
 */
public class FileListenerToYcl implements Runnable {

    public static Logger log = Logger.getLogger(FileListenerToYcl.class.getName());


    @Override
    public void run() {
        try {
            File fileDir = new File((String) RuntimeEnv.getParam(RuntimeEnv.catchFilePath));
            log.info("filePath----" + fileDir);
            if (fileDir.isDirectory()) {
                while (true) {
                    int i = 0;
                    File[] files = fileDir.listFiles();
                    ArrayList<String> fileList = new ArrayList();
                    for (File file : files) {
                        if (file.isFile()) {
                            fileList.add(file.getName());
                        }
                    }
                    for (File file : files) {
                        if (file.getName().contains(".ok")) {
                            if (fileList.contains(file.getName().split("[.]")[0])) {
                                i++;
                                try {
                                    //                                GlobalParas.filePool.put(file.getName().split("[.]")[0] + ".yw");
                                    FileUtils.moveFileToDirectory(new File((String) RuntimeEnv.getParam(RuntimeEnv.catchFilePath) + "/" + file.getName().split("\\.")[0]), new File((String) RuntimeEnv.getParam(RuntimeEnv.filePath)), true);
                                    FileUtils.moveFileToDirectory(new File((String) RuntimeEnv.getParam(RuntimeEnv.catchFilePath) + "/" + file.getName().split("\\.")[0] + ".ok"), new File((String) RuntimeEnv.getParam(RuntimeEnv.filePath)), true);
                                    log.info("mv file " + (String) RuntimeEnv.getParam(RuntimeEnv.catchFilePath)+ "/" + file.getName().split("\\.")[0] + " to path " + (String) RuntimeEnv.getParam(RuntimeEnv.filePath) + " successful");
                                } catch (Exception ex) {
                                    log.error(ex, ex);
                                }
                            }
                        }
                    }
                    System.currentTimeMillis();
                    log.info("get " + i + " file from the catchpath, will sleep...");
                    try {
                        Thread.sleep(3000);
                    } catch (InterruptedException ex) {
                    }
                    if (i == 0 && 0 == Integer.parseInt((String) RuntimeEnv.getParam(RuntimeEnv.writeJsonThreadSize))) {
                        log.info("will close this thread : " + Thread.currentThread().getName());
                        break;
                    }
                }
            }else{
                log.warn("no such file path "+(String) RuntimeEnv.getParam(RuntimeEnv.filePath));
            }
        } catch (Exception ex) {
            log.error(ex);
        }

    }
}
